package hamaster.jpbc;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "IBC_ID_KEY")
public class IDKey implements Serializable {
	private static final long serialVersionUID = -6993616341012572890L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer keyId;

	@OneToOne(optional = false)
	private User owner;

	@Lob
	private byte[] keyContent;

	@Column(nullable = false)
	@Temporal(value = TemporalType.TIMESTAMP)
	private Date genDate;

	public IDKey() {
	}

	public Integer getKeyId() {
		return keyId;
	}

	public void setKeyId(Integer keyId) {
		this.keyId = keyId;
	}

	public User getOwner() {
		return owner;
	}

	public void setOwner(User owner) {
		this.owner = owner;
	}

	public byte[] getKeyContent() {
		return keyContent;
	}

	public void setKeyContent(byte[] keyContent) {
		this.keyContent = keyContent;
	}

	public void setGenDate(Date genDate) {
		this.genDate = genDate;
	}

	public Date getGenDate() {
		return genDate;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((genDate == null) ? 0 : genDate.hashCode());
		result = prime * result + Arrays.hashCode(keyContent);
		result = prime * result + ((keyId == null) ? 0 : keyId.hashCode());
		result = prime * result + ((owner == null) ? 0 : owner.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		IDKey other = (IDKey) obj;
		if (genDate == null) {
			if (other.genDate != null)
				return false;
		} else if (!genDate.equals(other.genDate))
			return false;
		if (!Arrays.equals(keyContent, other.keyContent))
			return false;
		if (keyId == null) {
			if (other.keyId != null)
				return false;
		} else if (!keyId.equals(other.keyId))
			return false;
		if (owner == null) {
			if (other.owner != null)
				return false;
		} else if (!owner.equals(other.owner))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "IDKey [keyId=" + keyId + ", owner=" + owner + ", keyContent="
				+ Arrays.toString(keyContent) + ", genDate=" + genDate + "]";
	}

}
